/**
 * 对谷歌地图的进一步封装
 */

/* public
 * 构建地图
 */
function buildMap(mapid){
  var center = buildPoint(116.404,39.915);
  var mapProp = {
    center:center,
    zoom:11,
    mapTypeId:google.maps.MapTypeId.ROADMAP,
    fullscreenControl: false
  };
  map = new google.maps.Map(document.getElementById(mapid), mapProp);
  return map;
}

/* public
 * 构建自定义菜单栏
 */
function buildControl(controlDiv,position){
  var googlePostion = googlePostion = google.maps.ControlPosition.LEFT;
  if (position == "left") {
    googlePostion = google.maps.ControlPosition.LEFT;
  } else if(position == "right"){
    googlePostion = google.maps.ControlPosition.RIGHT;
  }
  map.controls[googlePostion].push(controlDiv);
}

/* public
 * 构建点(lng经度 lat纬度  传参顺序和百度地图保持一致)
 */
function buildPoint(lng,lat){
  return new google.maps.LatLng(lat,lng);
}

/* public
 * 创建点 通过xy
 */
function buildMarkerByPoint(point,icon,info_html) {
  var marker = new google.maps.Marker({
    position: point,
    map: map,
    icon : icon
  });
  if(info_html != undefined){
    var message = new google.maps.InfoWindow({  
      content : info_html,
      size : new google.maps.Size(500, 300)
    });
    google.maps.event.addListener(marker, 'click', function() {
      closeLastInfoWindow(message);
      message.open(map, marker);
      mapSetCenter(marker);
    });
  }
  return marker;
}

/*public 创建点+infowindow并且跳动*/
function buildMarkerByPointAndJump(point,icon,info_html){
    var marker = buildMarkerByPoint(point,icon,info_html);
    marker.setAnimation(1);
}

/* public
 * 创建点 通过xy
 */
function buildMarkerAndLabelByPoint(point,icon,label) {
  var marker = new google.maps.Marker({
    position: point,
    map: map,
    icon : icon,
    label : label
  });
  return marker;
}

/* public
 * 创建点 通过xy
 */
function buildMarkerByXY(lng,lat,icon) {
  var position = buildPoint(lng,lat);
  var marker = new google.maps.Marker({
    position: position,
    map: map,
    icon : icon
  });
  return marker;
}

/* public
 * 创建弹出框
 */
function buildInfoWindow(marker,content){
  var message = new google.maps.InfoWindow({  
    content : content
  });  
  google.maps.event.addListener(marker, 'click', function() {  
    message.open(map, marker);
    marker.setIcon(marker_red);
  });
  google.maps.event.addListener(marker, '', function() {  
    message.close(map, marker);  
    marker.setIcon(marker_light);
  });
}

/*展示所有点*/
function showAllPoints(screenPoints){
    if (screenPoints.length > 0) {
        var bounds = new google.maps.LatLngBounds();
        for(var item in screenPoints){
            var point = screenPoints[item];
            bounds.extend(point);
        }
        map.fitBounds(bounds);
    }
}

/*通过x,y获取街道信息*/
function getFormatted_address(lat,lng){
    var latLngData = lat.toFixed(6)+','+lng.toFixed(6);
    var url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" +latLngData + "&location_type=ROOFTOP&result_type=street_address&key=AIzaSyDC5XDdyZ9x3RV7sbZEMxpRju_sl6QPTaA";
    var address = unkounloaction;
    $.ajax({
        type: "get",
        url: url,
        dataType: "json",
        async:false,
        success: function(dataJson){
          var results = dataJson.results;
          if(results.length > 0){
              address = results[0].formatted_address;
          }
        }
    });
    
    return address;
}

/*构建热力图*/
function buildHotspot(points){
  var gradient = [
    'rgba(0, 255, 255, 0)',
    'rgba(0, 255, 255, 1)',
    'rgba(0, 191, 255, 1)',
    'rgba(0, 127, 255, 1)',
    'rgba(0, 63, 255, 1)',
    'rgba(0, 0, 255, 1)',
    'rgba(0, 0, 223, 1)',
    'rgba(0, 0, 191, 1)',
    'rgba(0, 0, 159, 1)',
    'rgba(0, 0, 127, 1)',
    'rgba(63, 0, 91, 1)',
    'rgba(127, 0, 63, 1)',
    'rgba(191, 0, 31, 1)',
    'rgba(255, 0, 0, 1)'
  ];
  heatmap = new google.maps.visualization.HeatmapLayer({
    data: points,
    map: map,
    radius : 20
  });
}

/*关闭热力图*/
function removeHeatmap(){
    heatmap.setMap(null);
}

/* 模拟点击marker */
function clickMarker(marker){
  new google.maps.event.trigger(marker, 'click');
}

/*---------------------------------------------地图点--------------------------------------------*/

//station  基站
var station_default = "/img/map/icon/station_default_small.png";
var station_light = "/img/map/icon/station_light_small.png";
var station_dark = "/img/map/icon/station_dark_small.png";
var station_red = "/img/map/icon/station_red_small.png";
var station_black = "/img/map/icon/station_black_small.png";

var station_default_big = "/img/map/icon/station_default_big.png";
var station_light_big = "/img/map/icon/station_light_big.png";
var station_dark_big = "/img/map/icon/station_dark_big.png";
var station_red_big = "/img/map/icon/station_red_big.png";
var station_black_big = "/img/map/icon/station_black_big.png";

//marker  正常点图标
var marker_default = "/img/map/icon/marker_default_small.png";
var marker_light = "/img/map/icon/marker_light_small.png";
var marker_dark = "/img/map/icon/marker_dark_small.png";
var marker_red = "/img/map/icon/marker_red_small.png";
var marker_black = "/img/map/icon/marker_black_small.png";

var marker_default_big = "/img/map/icon/marker_default_big.png";
var marker_light_big = "/img/map/icon/marker_light_big.png";
var marker_dark_big = "/img/map/icon/marker_dark_big.png";
var marker_red_big = "/img/map/icon/marker_red_big.png";
var marker_black_big = "/img/map/icon/marker_black_big.png";

//didi    滴滴图标
var didi_default = "/img/map/icon/didi_default_small.png";
var didi_red = "/img/map/icon/didi_red_small.png";

//黑夜白天
var marker_day = "/img/map/icon/marker_day.png";
var marker_night = "/img/map/icon/marker_night.png";
var marker_new = "/img/map/icon/marker_new.png";

//shadow --暂时随便用一个
var shadow_default = "/img/map/icon/station_default_small.png";

//开始结束图标
var marker_start = "/img/map/icon/start.png";
var marker_end = "/img/map/icon/end.png";